import found_get
import foundDayValue
import mon_calculate
import week_calculate
import DataBase
import datetime

dbutil = DataBase.DataBase('test')
try:
    #更新基金列表
    insert_sql = "insert into foundation (code,name,update_time,create_time) values (%s, %s, now(),now()) " \
                 "ON DUPLICATE KEY UPDATE name = VALUES(name),update_time=VALUES(update_time)"
    found_get =found_get.found_get()
    foundlist = found_get.get_new_foundlist1()
    dbutil.batch_insert(insert_sql, foundlist)

    #查询出状态为1的数据做增量同步，查询出状态为0的数据做全量同步

    found_get_sql = "select *from foundation where status = %s"
    insert_found_value_sql ="insert into foundation_values  (code,date,week,net_values,total_values," \
                            "day_rate,apply_state,Redeming_state,share_flag,convert_drifts,create_time) " \
                            "values (%s, %s,%s, %s,%s, %s,%s, %s,%s,%s,now())"
    update_status_sql = "update foundation set status=1,start_time=%s,end_time=%s where code=%s"#更新基金列表的基金状态和日期区间
    update_date_sql = "update foundation set end_time=%s where code=%s"#增量同步更新基金的结束日期
    #30天内涨跌幅计算
    insert_mon_calculate_sql = "insert into month_foundation_values (code,date,week,mon_rate,all_days,create_time)" \
                               "values (%s,%s,%s,%s,%s,now())"
    #7天内涨跌幅计算
    insert_week_calculate_sql = "insert into week_foundation_values (code,date,week,week_rate,all_days,create_time)" \
                               "values (%s,%s,%s,%s,%s,now())"

    #对新加入的基金做全量同步
    new_founds = dbutil.select_list(found_get_sql, 0)
    for element in new_founds:
        print("全量同步净值 code:"+element[0])
        value_records = foundDayValue.get_fund_data(element[0])#获取最新的每日净值数据
        condition = []
        #不存在数据
        if len(value_records)>0:
            condition.append(value_records[-1][1])#最新同步的日期最小值
            condition.append(value_records[0][1])#最新同步的日期最大值
            condition.append(element[0])#code
            dbutil.batch_insert(insert_found_value_sql, tuple(value_records))
            mon_records = mon_calculate.run(dbutil,tuple(condition))
            week_records = week_calculate.run(dbutil,tuple(condition))
            dbutil.batch_insert(insert_mon_calculate_sql,mon_records)
            dbutil.batch_insert(insert_week_calculate_sql, week_records)
            dbutil.update_one(update_status_sql,tuple(condition))

    #对已存在的基金做增量同步
    old_founds = dbutil.select_list(found_get_sql, 1)
    for element in old_founds:
        #[code,name,status,start_time,end_time,update_time,create_time]
        print("增量同步净值 code:" + element[0])
        #获取已有记录最晚的时间
        start_date =  element[4]
        #获取后一天
        #先转换为时间类型
        start_date = datetime.datetime.strptime(start_date,'%Y-%m-%d')
        #加一天在转换为字符类型
        start_date = (start_date + datetime.timedelta(days=1)).strftime('%Y-%m-%d')
        value_records = foundDayValue.get_fund_data(code=element[0],start=start_date)  # 获取最新的每日净值数据
        if len(value_records)>0:
            condition = []
            condition.append(value_records[-1][1])  # 最新同步的日期最小值
            condition.append(value_records[0][1])  # 最新同步的日期最大值
            condition.append(element[0])  # code
            dbutil.batch_insert(insert_found_value_sql, tuple(value_records))
            mon_records = mon_calculate.run(dbutil, tuple(condition))
            week_records = week_calculate.run(dbutil, tuple(condition))
            dbutil.batch_insert(insert_mon_calculate_sql, mon_records)
            dbutil.batch_insert(insert_week_calculate_sql, week_records)
            update_condition = []
            update_condition.append(value_records[0][1])  # 最新同步的日期最大值
            update_condition.append(element[0])  # code
            # 更新数据库中最晚的时间
            dbutil.update_one(update_date_sql, tuple(update_condition))

except Exception as e:
    raise
finally:
    dbutil.close_conn()